package com.neusoft.ecommerce.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.neusoft.ecommerce.entity.Indent;
import com.neusoft.ecommerce.service.IndentService;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.neusoft.ecommerce.dbhelper.LayuiPager;
import com.neusoft.ecommerce.entity.AfterSaleService;
import com.neusoft.ecommerce.service.AfterSaleServiceService;

/**
 * 
 * @author YinKuenTam
 *
 */
@RestController
public class AfterSaleServiceController {
	private final AfterSaleServiceService afterSaleServiceService;
	private final IndentService indentService;
	
	public AfterSaleServiceController(AfterSaleServiceService afterSaleServiceService, IndentService indentService) {
		this.afterSaleServiceService = afterSaleServiceService;
		this.indentService = indentService;
	}
	
	@GetMapping("/afterSaleService_page")
    public ModelAndView afterSaleServicePage() {
        System.out.println("-----sss______");
         return new ModelAndView("afterSaleService/afterSaleService-list");
    }
	
    @RequestMapping("/afterSaleService/list")
    @ResponseBody
    public LayuiPager<AfterSaleService> list(@RequestParam(value = "page", defaultValue = "1") String page,
                                    @RequestParam(value = "limit", defaultValue = "10") String limit) {
        System.out.println(page + "" + limit);
        LayuiPager<AfterSaleService> pager = new LayuiPager<>(Integer.parseInt(page), Integer.parseInt(limit));
        int count = afterSaleServiceService.findTotal(pager);
        List<AfterSaleService> list = afterSaleServiceService.findList(pager);
        for (AfterSaleService afterSaleService : list) {
            System.out.println(afterSaleService);
        }
        pager.setData(list);
        pager.setCount(count);
        pager.setCode(0);
        pager.setMsg("");
        return pager;
    }
	
	@GetMapping("/addAfterSaleService")
    public ModelAndView addAfterSaleService() {
        return new ModelAndView("afterSaleService/afterSaleService-add");
    }
	
	@RequestMapping("/afterSaleService/indentList")
    public List<Indent> findIndentList() {
	    return indentService.findAll();
    }
	
	@PostMapping("/afterSaleService/add")
    public Map<String, Object> add(AfterSaleService afterSaleService) {
        System.out.println(afterSaleService);
        Map<String, Object> map = new HashMap<>(16);
        if (afterSaleServiceService.add(afterSaleService)) {
            map.put("msg","ok");
            map.put("code",0);
        } else {
            map.put("msg","失败");
            map.put("code",1);
        }
        return map;
    }
	
	@GetMapping("/afterSaleService/edit_page")
    public ModelAndView editPage(AfterSaleService afterSaleService, Model model) {
        System.out.println(afterSaleService);
        LayuiPager<AfterSaleService> pager = new LayuiPager<>(1,1);
        pager.setCondition(afterSaleService);
        AfterSaleService message = afterSaleServiceService.findList(pager).get(0);
        System.out.println(message);
        ModelAndView view = new ModelAndView("afterSaleService/afterSaleService-edit");
        model.addAttribute("afterSaleService", message);
        return view;
    }


	@PostMapping("/afterSaleService/edit")
    public Map<String, Object> edit(AfterSaleService afterSaleService) {
        System.out.println(afterSaleService);
        Map<String, Object> map = new HashMap<>(16);
        if (afterSaleServiceService.edit(afterSaleService)) {
            map.put("msg","ok");
            map.put("code",0);
        } else {
            map.put("msg","失败");
            map.put("code",1);
        }
        return map;
    }
	@PostMapping("/afterSaleService/delete")
    public Map<String, Object> delete(AfterSaleService afterSaleService) {
        System.out.println(afterSaleService);
        Map<String, Object> map = new HashMap<>(16);
        if (afterSaleServiceService.delete(afterSaleService)) {
            map.put("msg","ok");
            map.put("code",0);
        } else {
            map.put("msg","失败");
            map.put("code",1);
        }
        return map;
    }
	
	

}
